﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class LevelCenterButton : MonoBehaviour
{
    public TextButton centerBtn;
    public List<TextButton> otherTBList = new List<TextButton>();
    public Text loveText;
    public GameObject rootObj;
    public GameLevelItem gameLevelItem;

    // Start is called before the first frame update
    void Start()
    {
        InitTB();
        loveText.DOFade(0, 0);
    }

    private void InitTB()
    {
        centerBtn.Init(string.Empty, () =>
        {
            TextButton tb = GetRandomTB();
            if (tb.Equals(centerBtn))
            {
                centerBtn.button.image.enabled = false;
                centerBtn.button.enabled = false;
                Invoke("ShowLoveText", 0.5f);
            }
            else
            {
                tb.button.image.enabled = false;
                tb.button.enabled = false;
            }

        });

        int count = otherTBList.Count;
        for (int i = 0; i < count; i++)
        {
            otherTBList[i].Init(string.Empty, () => { });
        }
    }

    public TextButton GetRandomTB()
    {
        int count = otherTBList.Count;
        TextButton tb = null;
        if (count == 0)
            tb = centerBtn;
        else
        {
            int randomIndex = Random.Range(0, count);
            tb = otherTBList[randomIndex];
            otherTBList.RemoveAt(randomIndex);
        }
        return tb;
    }

    private void ShowLoveText()
    {
        loveText.DOFade(1, 0.5f);
        rootObj.transform.DOScale(Vector3.zero, 0.5f);
        Invoke("TriggerResult", 0.5f);
    }

    private void TriggerResult()
    {
        gameLevelItem.TriggerResult(true);
    }



}
